package curves.gui;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;

public class PointMenu extends AbstractSurfaceMenu {

    private GuiPoint targetPoint;
    private AlgorithmDialog algorithmDialog;
    private WeightDialog weightDialog;
    private JMenuItem lineOpener;

    public PointMenu(Window window) {
        super(window);
        JMenuItem menuItem = new JMenuItem("Set weight");
        menuItem.addActionListener(new SetPointWeightAction());
        add(menuItem);
        addSeparator();


        menuItem = new JMenuItem("Remove point");
        menuItem.addActionListener(new RemovePointAction());
        add(menuItem);
        addSeparator();

        menuItem = new JMenuItem("Remove curve");
        menuItem.addActionListener(new RemoveCurveAction());
        add(menuItem);


        lineOpener = new JMenuItem("Close curve");
        lineOpener.addActionListener(new LineOpenerAction());
        add(lineOpener);
        addSeparator();


        menuItem = new JMenuItem("Choose algorithm");
        menuItem.addActionListener(new ChangeAlgorithmAction());
        add(menuItem);
    }

    class LineOpenerAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (getSurface().getCurrentCurve().isOpen()) {
                getSurface().getCurrentCurve().setOpen(false);
                lineOpener.setText("Open curve");
            } else {
                getSurface().getCurrentCurve().setOpen(true);
                lineOpener.setText("Close curve");
            }
            frame.getGraphicEngine().repaint();
        }
    }

    class SetPointWeightAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (weightDialog == null) {
                weightDialog = new WeightDialog(frame, getSurface(), getTargetPoint().getPoint());
            }
            Point location = MouseInfo.getPointerInfo().getLocation();
            location.setLocation(location.getX() - weightDialog.getWidth() / 2.0, location.getY() - weightDialog.getHeight() / 2.0);
            weightDialog.setLocation(location);
            weightDialog.setTargetPoint(getTargetPoint().getPoint());
            weightDialog.setVisible(true);
            frame.getGraphicEngine().repaint();
        }
    }

    class ChangeAlgorithmAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (algorithmDialog == null) {
                algorithmDialog = new AlgorithmDialog(frame, getSurface());
            }
            Point location = MouseInfo.getPointerInfo().getLocation();
            location.setLocation(location.getX() - algorithmDialog.getWidth() / 2.0, location.getY() - algorithmDialog.getHeight() / 2.0);
            algorithmDialog.setLocation(location);
            String algo = getSurface().getCurrentCurve().getCurveAlgorithm().toString();
            algorithmDialog.setSelectedAlgorithm(algo);
            algorithmDialog.setVisible(true);
            frame.getGraphicEngine().repaint();
        }
    }

    public GuiPoint getTargetPoint() {
        return targetPoint;
    }

    public void setTargetPoint(GuiPoint targetPoint) {
        this.targetPoint = targetPoint;
    }

    class RemovePointAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            getSurface().getCurrentCurve().removePoint(getTargetPoint().getPoint());
            frame.getGraphicEngine().repaint();
        }
    }

    class RemoveCurveAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            getSurface().getCurves().remove(getSurface().getCurrentCurve());
            frame.getGraphicEngine().repaint();
        }
    }

    public void updateLabels() {
        if (getSurface().getCurrentCurve().isOpen()) {
            lineOpener.setText("Close curve");
        } else {
            lineOpener.setText("Open curve");
        }
    }
}
